package com.ljh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljh.pojo.Menu;
import com.ljh.pojo.MenuRole;
import com.ljh.pojo.Role;
import com.ljh.service.IMenuRoleService;
import com.ljh.service.IMenuService;
import com.ljh.service.IRoleService;
import com.ljh.utils.RespBean;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZSJia
 * @date 2021/8/13 013 - 13:16
 */
@RestController
@RequestMapping("/system/basic/permiss")
public class PermissController {

    @Autowired
    private IRoleService roleService;
    @Autowired
    private IMenuService menuService;
    @Autowired
    private IMenuRoleService menuRoleService;

    @ApiOperation(value = "查找所有角色")
    @GetMapping("/")
    public List<Role> findListRole(){
        return roleService.list();
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("/")
    public RespBean addRole(@RequestBody Role role){
        role.setGmtCreate(LocalDateTime.now());
        if (!role.getCode().startsWith("ROLE_")){
            role.setCode("ROLE_"+role.getCode());
        }
        if (roleService.save(role)){
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }


    @ApiOperation(value = "修改角色")
    @PutMapping("/")
    public RespBean updateRole(@RequestBody Role role){
        if (!role.getCode().startsWith("ROLE_")){
            role.setCode("ROLE_"+role.getCode());
        }
        if (roleService.updateById(role)){
            return RespBean.success("修改成功");
        }
        return RespBean.error("修改失败");
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("/role/{rid}")
    public RespBean updateRole(@PathVariable Integer  rid){
        if (roleService.removeById(rid)){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "删除多个角色")
    @DeleteMapping("/")
    public RespBean updateRole( Integer[] ids){
        if (roleService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/menu/")
    public List<Menu> findListMenu(){
        return  menuService.getAllMenus();
    }

    @ApiOperation(value = "根据角色id查询菜单id")
    @GetMapping("/mid/{rid}")
    public List<Integer> getMidByAdminId(@PathVariable Integer rid){

        return  menuRoleService.list(new QueryWrapper<MenuRole>().eq("rid",rid))
                                                        .stream()
                                                        .map(MenuRole::getMid)
                                                        .collect(Collectors.toList());
    }


        @ApiOperation(value = "更新角色菜单")
        @PutMapping("/updMenu")
    public RespBean updateMenuRole(Integer rid,Integer[] mids){
        return menuRoleService.updateMenuRole(rid,mids);

}

}
